package com.xinqi.modules.sys.textbook.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentCreateDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentQueryDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentReplaceDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentUpdateDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookContentResultDTO;
import com.xinqi.modules.sys.textbook.service.CloudTextbookContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 云课本课堂内容ADMIN控制层
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Tag(name = "Admin-云课本", description = "云课本Admin接口")
@RestController
@RequestMapping("/admin/cloud-textbook")
@RequiredArgsConstructor
public class CloudTextbookContentAdminController {
    private final CloudTextbookContentService cloudTextbookContentService;

    /**
     * 云课本课堂内容-分页
     */
    @Operation(summary = "云课本课堂内容-分页")
    @GetMapping("/{textbookId}/content/page")
    public R<PageInfo<CloudTextbookContentResultDTO>> page(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                                           @ParameterObject CloudTextbookContentQueryDTO dto) {
        dto.setBookId(textbookId);
        PageInfo<CloudTextbookContentResultDTO> page = cloudTextbookContentService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 云课本课堂内容-详情
     */
    @Operation(summary = "云课本课堂内容-详情")
    @GetMapping("/{textbookId}/content/{id}")
    public R<CloudTextbookContentResultDTO> findById(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                                     @Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        CloudTextbookContentResultDTO dto = cloudTextbookContentService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增云课本课堂内容
     */
    @Operation(summary = "新增云课本课堂内容")
    @PostMapping("/{textbookId}/content")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增云课本课堂内容", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                          @RequestBody CloudTextbookContentCreateDTO dto) {
        dto.setCloudTextbookId(textbookId);
        return R.data(cloudTextbookContentService.insert(dto));
    }

    /**
     * 批量新增云课本课堂内容
     */
    @Operation(summary = "批量新增云课本课堂内容")
    @PostMapping("/{textbookId}/content/batch")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增云课本课堂内容", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Boolean> batch(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                            @RequestBody List<CloudTextbookContentCreateDTO> list) {
        Optional.ofNullable(list).ifPresent(l -> l.forEach(dto -> dto.setCloudTextbookId(textbookId)));
        return R.data(cloudTextbookContentService.insertBatch(list));
    }

    /**
     * 批量新增云课本课堂内容
     */
    @Operation(summary = "关联/替换 云课本课堂内容")
    @PutMapping("/{textbookId}/content/ref")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 关联/替换云课本课堂内容", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> replace(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                              @RequestBody List<CloudTextbookContentReplaceDTO> list) {
        return R.data(cloudTextbookContentService.replace(textbookId, list));
    }

    /**
     * 修改云课本课堂内容
     */
    @Operation(summary = "修改云课本课堂内容")
    @PutMapping("/{textbookId}/content/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改云课本课堂内容", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                             @Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody CloudTextbookContentUpdateDTO dto) {
        dto.setId(id);
        return R.data(cloudTextbookContentService.updateById(dto));
    }

    /**
     * 删除云课本课堂内容
     */
    @Operation(summary = "删除云课本课堂内容")
    @DeleteMapping("/{textbookId}/content/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除云课本课堂内容", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                             @PathVariable("id") Long id) {
        return R.data(cloudTextbookContentService.deleteById(id));
    }

}
